package com.bw.repair.page

import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels

import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.base.path.RouterPath
import com.bw.common.event.CustomEvent
import com.bw.common.view.BaseMvvmActivity
import com.bw.repair.R
import com.bw.repair.adapter.RepairAdapter
import com.bw.repair.databinding.ActivityRepairBinding
import com.bw.repair.vm.RepairViewModel
import dagger.hilt.android.AndroidEntryPoint
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@Route(path = RouterPath.PATH_REPAIR_PATH)
@AndroidEntryPoint
class RepairActivity : BaseMvvmActivity<ActivityRepairBinding>() {
    override fun getLayoutId(): Int  = R.layout.activity_repair
    private val mViewModel:RepairViewModel by viewModels()
    val mAdapter = RepairAdapter()
    override fun initViews() {
        super.initViews()
        mDataBinding.repairRv.adapter = mAdapter

        //跳转到添加页面
        mDataBinding.addRepair.setOnClickListener {
            ARouter.getInstance().build(RouterPath.PATH_ADD_REPAIR_PATH).navigation()
        }
        //返回
        mDataBinding.back.setOnClickListener { finish() }
    }

    override fun loadData() {
        super.loadData()
        mViewModel.getRepairData()
    }

    override fun observeDataChanged() {
        super.observeDataChanged()
        mViewModel.mRepairSuccess.observe(this){
            mAdapter.data.clear()
            mAdapter.data.addAll(it.reversed())
            mAdapter.notifyDataSetChanged()
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onCusEventBus(event: CustomEvent){
        if (event.target == RouterPath.PATH_REPAIR_PATH){
            when(event.type){
                1->{
                    Log.d("-ss","123")
                    mViewModel.getRepairData()
                }
            }
        }
    }



}